package com.mebs.model.service.procurement;

import com.mebs.model.CurrencyName;
import com.mebs.model.Country;
import com.mebs.model.AuditInfo;

import javax.persistence.*;
import java.util.Date;

import org.jt.jet.annotation.Field;
import org.jt.jet.annotation.ArgumentType;
import static org.hibernate.proxy.HibernateProxyHelper.getClassWithoutInitializingProxy;

/**
 * Created by Sergey Prasolov, sap@jensentechnologies.com
 * Date: Mar 14, 2006, 2:53:47 PM
 */
@Entity()
@Table(name = "QUOTE")
public class Quote {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "Id")
    private Long id;

    @ManyToOne(optional = false)
    @JoinColumn(name = "commodityId")
    @Field(type = ArgumentType.INSTANCE)
    private Commodity commodity;

    @ManyToOne(optional = false)
    @JoinColumn(name = "SupplierId")
    private Supplier supplier;

    @Column(name = "Value", nullable = false)
    private Double value;

    @Column(name = "AdditionalCharges", nullable = true)
    private Double additionalCharges;

    @ManyToOne(optional = false)
    @JoinColumn(name = "CurrencyNameId")
    @Field(type = ArgumentType.INSTANCE)
    private CurrencyName currencyName;

    @ManyToOne(optional = true)
    @JoinColumn(name = "OriginCountryId")
    @Field(type = ArgumentType.INSTANCE)
    private Country originCountry;

    @Column(name = "DateOfOffer", nullable = false)
    private Date dateOfOffer;

    @Embedded
    private AuditInfo auditInfo;

    /**
     * **********************
     * Getters-setters
     * ***********************
     */

    public Long getId()
    {
        return id;
    }

    public void setId(Long id)
    {
        this.id = id;
    }

    public Commodity getCommodity()
    {
        return commodity;
    }

    public void setCommodity(Commodity commodity)
    {
        this.commodity = commodity;
    }

    public Supplier getSupplier()
    {
        return supplier;
    }

    public void setSupplier(Supplier supplier)
    {
        this.supplier = supplier;
    }

    public Double getValue()
    {
        return value;
    }

    public void setValue(Double value)
    {
        this.value = value;
    }

    public Double getAdditionalCharges()
    {
        return additionalCharges;
    }

    public void setAdditionalCharges(Double additionalCharges)
    {
        this.additionalCharges = additionalCharges;
    }

    public CurrencyName getCurrencyName()
    {
        return currencyName;
    }

    public void setCurrencyName(CurrencyName currencyName)
    {
        this.currencyName = currencyName;
    }

    public Country getOriginCountry()
    {
        return originCountry;
    }

    public void setOriginCountry(Country originCountry)
    {
        this.originCountry = originCountry;
    }

    public Date getDateOfOffer()
    {
        return dateOfOffer;
    }

    public void setDateOfOffer(Date dateOfOffer)
    {
        this.dateOfOffer = dateOfOffer;
    }

    public AuditInfo getAuditInfo()
    {
        if (this.auditInfo == null) {
            this.auditInfo = new AuditInfo();
        }
        return auditInfo;
    }

    public void setAuditInfo(AuditInfo auditInfo)
    {
        this.auditInfo = auditInfo;
    }

    @Transient
    public Double getTotalQuoteValue() {
        return this.commodity.getQuantity() * this.value;
    }

    @Transient
    public boolean isDeletable()
    {
        return !this.equals(commodity.getQuote());
    }

    @Override
    public String toString()
    {
        if(this.value != null) {
            String retValue = this.value.toString();
            if(this.currencyName != null) {
                retValue += " " + this.currencyName.toString();
            }
            return  retValue;
        } else {
            return "";
        }
    }

    @Override
    public int hashCode()
    {
        if (id == null) return super.hashCode();
        return id.hashCode();
    }

    @Override
    public boolean equals(Object o)
    {
        if (o == null) return false;
        if (this == o) return true;
        if (id == null) return false;
        if (getClassWithoutInitializingProxy(this) != getClassWithoutInitializingProxy(o))
            return false;
        final Quote that = (Quote) o;
        return id.equals(that.getId());
    }

}
